package Day07;

import java.util.Arrays;
import java.util.Scanner;

public class AtmTest {
    public static void main(String[] args) {
        String[][] users ={{"admin","admin","10000","10","是"},
                           {"小林","123456","10001","10","是"}};
        Scanner scanner = new Scanner(System.in);
        String choose,regName,regPsw,cars,inputmoney,inputcars;
        String[] loginuser = new String[5];
        String[] carsarr = new String[5];
        boolean regNameRhgit = false;
        boolean isadmin = false;
        boolean boolregName = false;
        int cardnum = 10001;
     reg:   while (true){
            System.out.println("欢迎来到**银行");
            System.out.println("请输入你要办理的业务");
            System.out.println("1.登录");
            System.out.println("2.开卡");
            System.out.println(("3.退出"));
            isadmin = false;
            regNameRhgit = false;
            choose=scanner.next();
            if("1".equals(choose)){
              login:  while (true){
                    System.out.println("请输入你的账号：");
                    regName = scanner.next();
                    for (String[] user : users) {
                        if(regName.equals(user[0])){
                            regNameRhgit = true;
                            loginuser = user;
                        }
                    }
                  if(!regNameRhgit){
                      System.out.println("你输入的账号有错，请重新输入。");
                      continue login;
                  }
                    if(loginuser[4].equals("否")){
                        System.out.println("你的账号未激活，请联系管理员。");
                        continue reg;
                    }
                  System.out.println("请输入密码：");
                    regPsw = scanner.next();
                    if(!loginuser[1].equals(regPsw)){
                        System.out.println("密码输入错误，请重新输入");
                        continue login;
                    }
                    if("admin".equals(regName)){
                        isadmin = true;
                    }
                  System.out.println("登录成功");
                  System.out.println("欢迎"+(isadmin?"管理员登录":"用户"+regName+"登录"));
                  break login;
                }
                if(isadmin){
                  jh:  while (true){
                        System.out.println("1.查看所有用户");
                        System.out.println("2.查看所有需要被激活的用户");
                        System.out.println("3.激活用户");
                        System.out.println("4.冻结用户");
                        System.out.println("5.退出");
                        choose = scanner.next();
                        switch (choose){
                            case "1":
                                System.out.println("用户\t卡号\t余额\t激活状态");
                                for (String[] user : users) {
                                    System.out.println(user[0]+"\t"+user[2]+"\t"+user[3]+"\t"+user[4]);
                                }
                                continue jh;
                            case "2":
                                System.out.println("用户/t卡号/t余额/t激活状态");
                                for (String[] user : users) {
                                    if(user[4].equals("否")){
                                        System.out.println(user[0]+"\t"+user[2]+"\t"+user[3]+"\t"+user[4]);
                                    }
                                }
                               continue jh;
                            case "3":
                           bool:   while (true) {
                               boolregName = false;
                                  System.out.println("请输入需要被激活的用户名：");
                                  regName = scanner.next();
                                  for (String[] user : users) {
                                      if(user[0].equals(regName)){
                                          boolregName = true;
                                          carsarr = user;
                                      }
                                      }
                               if(!boolregName){
                                   System.out.println("输入的用户名有错，请重新输入");
                                   continue bool;
                                  }
                               System.out.println("请输入需要被激活的卡号：");
                                 cars = scanner.next();
                               if(!carsarr[2].equals(cars)){
                                   System.out.println("输入的卡号有误。");
                               }
                               else {
                                   carsarr[4] = "是";
                                   System.out.println("恭喜你，激活成功。");
                               }

                                  continue jh;
                              }
                            case "4":
                             ice:   while (true) {
                                    boolregName = false;
                                    System.out.println("请输入需要被冻结的用户名：");
                                    regName = scanner.next();
                                    for (String[] user : users) {
                                        if(user[0].equals(regName)){
                                            boolregName = true;
                                            carsarr = user;
                                        }

                                        }
                                 if(!boolregName){
                                     System.out.println("输入的用户名有错，请重新输入");
                                     continue ice;
                                    }
                                    System.out.println("请输入需要被冻结的卡号：");
                                    cars = scanner.next();
                                    if(!carsarr[2].equals(cars)){
                                        System.out.println("输入的卡号有误。");
                                    }
                                    else {
                                        carsarr[4] = "否";
                                    }
                                    System.out.println("账号已被冻结。");
                                    continue jh;
                                }
                            case "5":
                                break ;
                        }
                      break ;
                    }


                }
                else {
                  a:  while (true) {
                        System.out.println("1.查看余额。");
                        System.out.println("2.存钱");
                        System.out.println("3.转账");
                        System.out.println("4.查看个人信息");
                        System.out.println("5.退出");
                        choose = scanner.next();
                        switch (choose){
                            case "1":
                                System.out.println("你的余额为："+loginuser[3]);
                                break ;

                            case "2":
                                System.out.println("请输入你要存入的金额:");
                                inputmoney = scanner.next();
                                int money =Integer.valueOf(loginuser[3]);
                                money = money+ Integer.valueOf(inputmoney);
                                loginuser[3] = money+"";
                                System.out.println("恭喜你存款成功。");
                                break ;
                            case "3":
                                System.out.println("请输入你要转账的名字：");
                                regName = scanner.next();
                                System.out.println("请输入你要转账的卡号：");
                                inputcars = scanner.next();
                                for (String[] user : users) {
                                    if(user[0].equals(regName)&&user[2].equals(inputcars)){
                                        System.out.println("请输入你要转出的金额:");
                                        inputmoney = scanner.next();
                                        if(Integer.valueOf(loginuser[3])<Integer.valueOf(inputmoney)){
                                            System.out.println("你的余额不足，请重新输入。");
                                            continue a;
                                        }
                                        loginuser[3] =String.valueOf( Integer.valueOf(loginuser[3])-Integer.valueOf(inputmoney));
                                        user[3] = String.valueOf(Integer.valueOf(user[3])+Integer.valueOf(inputmoney));
                                        System.out.println("转账成功。");
                                        continue a;
                                    }
                                }
                                System.out.println("输入的信息有误");
                                break ;

                            case "4":
                                System.out.println("用户\t密码\t卡号\t余额\t激活状态");
                                System.out.println(loginuser[0]+"\t"+loginuser[1]+"\t"+loginuser[2]+"\t"+loginuser[3]+"\t"+loginuser[4]);
                            case "5":
                                continue reg;
                            default:
                                System.out.println("你选择的服务有误，请重新输入：");
                                continue a;
                        }
                    }

                }



            }
            else if("2".equals(choose)){
              begin:  while (true){
                  System.out.println("请输入你的用户名");
                    regName = scanner.next();
                    for (String[] user:users){
                        if(regName.equals(user[0])){
                            System.out.println("对不起，用户已经存在,请重新输入");
                           continue begin;
                        }
                    }
                  System.out.println("请输入密码");
                    regPsw = scanner.next();
                    cardnum++;
                    users = Arrays.copyOf(users,users.length+1);
                    String[] newuser = {regName,regPsw,cardnum+"","20","否"};
                    users[users.length-1] = newuser;
                  System.out.println("开卡成功");
                  for (String[] user:users){
                      System.out.println(Arrays.toString(user));
                  }
                  continue reg;

                }

            }
        }
    }
}
